package com.sxt.practiceapplication;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

import com.sxt.practiceapplication.activities.GuideActivity;
import com.sxt.practiceapplication.activities.MainActivity;
import com.sxt.practiceapplication.base.BaseActivity;
import com.sxt.practiceapplication.utils.CacheUtils;

public class SplashActivity extends BaseActivity {

    public static final String START_MAIN = "start_main";
    private boolean isStartMain;
    private final Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        RelativeLayout rl_splash_root = findViewById(R.id.rl_splash_root);


        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);
        scaleAnimation.setFillAfter(true);


        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setAnimationListener(new Animation.AnimationListener() {


            @Override
            public void onAnimationStart(Animation animation) {
                isStartMain = CacheUtils.getBoolean(SplashActivity.this, START_MAIN);

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                handler.postDelayed(() -> {
                    //判断是否进入过主页面
                    toActivity(isStartMain);
                }, 1000);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        rl_splash_root.startAnimation(animationSet);

    }

    private void toActivity(boolean isStartMain) {
        // 如果进入过直接进入主页面
        Intent intent;
        if (isStartMain) {
            intent = new Intent(SplashActivity.this, MainActivity.class);
        } else {// 否则进入引导页面
            intent = new Intent(SplashActivity.this, GuideActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        toActivity(isStartMain);
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}